package CtrLayer;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import ModelLayer.CustomerCollection;
import ModelLayer.Group;
import ModelLayer.Customer;
import ModelLayer.Stock;

/**
 * Write a description of class CustomerCtr here.
 * 
 * @author (your name)
 * @version (a version number or a date)
 */
public class CustomerCtr
{

	/**
	 * Constructor for objects of class CustomerCtr
	 */
	public CustomerCtr()
	{

	}

	/**
	 * An example of a method - replace this comment with your own
	 * 
	 * @param y
	 *            a sample parameter for a method
	 * @return the sum of x and y
	 */
	public String add_customer(String name, String phone_nr, String address,
			String postal_code, String city, String group)
	{
		return CustomerCollection.add_customer(name, phone_nr, address,
				postal_code, city, group);
	}

	public boolean delete_customer(String id)
	{
		return CustomerCollection.delete_customer(id);
	}

	public boolean existing_customer(String id)
	{
		return CustomerCollection.existing_customer(id);
	}
	public String view_customer(String id)
	{
		return CustomerCollection.find_customer(id).printable();
	}
	
	public Object[][] display_all_customers()
	{
		return CustomerCollection.display_all_customers();
	}
	
	public Object[] find_customer_by_id(String id)
	{
		if (CustomerCollection.find_customer(id)!=null)
			return CustomerCollection.find_customer(id).give_array();
		else
			return null;
	}
	public boolean is_such_customer(String id)
	{
		return !(CustomerCollection.find_customer(id)==null);
	}
	
	public Object[] find_customer_by_name(String name)
	{
		for (Customer temp: CustomerCollection.get_Customers())
		{
			if (name.equals(temp.get_name()))
			{
				return temp.give_array();
			}
		}
		return null;
	}
	
	public String[] give_string_array(String id)
	{
		return CustomerCollection.find_customer(id).give_string_data().split("\t");
	}
	public String[] give_string_group(String name)
	{
		return CustomerCollection.get_customer_groups().get(name).give_string_data().split("\t");
	}
	
	public boolean update_customer(String id, String name, String phone_nr,
			String address, String postal_code, String city, String group)
	{
		return CustomerCollection.update_customer(id, name, phone_nr, address,
				postal_code, city, group);
	}

	public boolean is_group(String name)
	{
		return CustomerCollection.is_group(name);
	}

	public boolean is_group_used(String name)
	{
		return CustomerCollection.is_group_used(name);
	}

	public boolean add_group(String name, String terms, double discount)
	{
		return CustomerCollection.add_group(name, terms, discount);
	}

	public String find_group(String name)
	{
		return CustomerCollection.view_group(name);
	}

	public boolean update_group(String name, String terms, double discount)
	{
		return CustomerCollection.update_group(name, terms, discount);
	}

	public boolean delete_group(String name)
	{
		return CustomerCollection.delete_group(name);
	}

	public String print_groups()
	{
		String all = "";
		Iterator<Map.Entry<String, Group>> new_it = CustomerCollection
				.get_customer_groups().entrySet().iterator();
		while (new_it.hasNext())
		{
			all = all.concat(CustomerCollection.view_group(new_it.next()
					.getKey()));
			all = all.concat("\n\n");
		}
		return all;
	}
	public String[] all_groups2()
	{
		String[] keys={"0"};
		int i=0;
		Iterator<Map.Entry<String, Group>> new_it = CustomerCollection.get_customer_groups().entrySet().iterator();
		while (new_it.hasNext())
		{
			
			keys[i]=CustomerCollection.view_group(new_it.next().getKey());
			i++;
		}
		return keys;
	}
	public String[] all_groups()
	{
		
		Set<String> myset = CustomerCollection.get_customer_groups().keySet();
		String[] keys= myset.toArray(new String[0]);
		return keys;
	}
	public Object[] display_group(String name)
	{
		if (CustomerCollection.get_customer_groups().get(name)!=null)
		{
			return CustomerCollection.get_customer_groups().get(name).give_array();
		}
		else 
		{
			return null;
		}
	}
	public Object[][] display_all_groups()
	{
		return CustomerCollection.display_all_groups();
	}
	public boolean existing_groups()
	{
		return CustomerCollection.get_customer_groups().isEmpty();
	}

	public void save_data()
	{
		CustomerCollection.save_all_data();
	}
	public String get_next_cust_nr()
	{
		return CustomerCollection.get_next_cust_nr();
	}
}
